<template>
  <div>
    <div>
      <FormPanel
        ref="formPanel"
        :list="formList"
        :cols="2"
        :formType="type"
        labelWidth="120"
        :isSubmitBtn="false"
        @formChange="formChangeHandle"
      />
    </div>
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'center'
      }"
    >
      <el-button @click="closeDialog">取 消</el-button>
      <AjaxButton :click="saveDialog" type="primary">保 存</AjaxButton>
    </div>
  </div>
</template>

<script>
import { addTestDriveWay, editQueryTestDriveWay, editTestDriveWay } from '@/api/potenCus/basedata/searchTestDriveWays';
import { dictionary } from '@/utils/dictMixin';

export default {
  name: 'SeriesModal',
  mixins: [dictionary],
  props: {
    type: {
      type: String,
      default: 'add'
    },
    rowData: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    this.saveData = null;
    return {
      formList: this.createFormList()
    };
  },
  created() {
    this.init();
  },
  methods: {
    init() {
      // this.formList.find(x => x.fieldName === 'rank').offsetRightCols = 1;
      if (this.type === 'edit') {
        // 更新接口需要的记录 ID
        this.recordId = this.rowData.wayId;
        // this.formList.splice(1, 0, {
        //   type: 'SELECT',
        //   label: '是否有效',
        //   fieldName: 'isValid',
        //   placeholder: '请选择',
        //   style: { width: '200px' },
        //   itemList: this.createDictList('1004'),
        //   rules: [{ required: true, message: '请选择是否有效', trigger: 'blur' }]
        // })
        let arr = [];
        arr.push(this.rowData.wayPicture)
        this.rowData.wayPicture = arr
        this.formList.forEach(x => {
          x.initialValue = this.rowData[x.fieldName];
        });
      }
    },
    createFormList() {
      return [
        {
          type: 'INPUT',
          label: '路线名称',
          fieldName: 'wayName',
          placeholder: '路线名称',
          maxlength: 16,
          rules: [{ required: true, message: '请填写路线名称', trigger: 'blur' }]
        },
        {
          type: 'SELECT',
          label: '是否有效',
          fieldName: 'isValid',
          placeholder: '请选择',
          itemList: this.createDictList('1004'),
          rules: [{ required: true, message: '请选择是否有效', trigger: 'change' }]
        },
        {
          type: 'INPUT_NUMBER',
          label: '线路时间(分钟)',
          fieldName: 'testDriveWayTime',
          placeholder: '请输入',
          min: 1,
          max: 999,
          editType: 'number',
          precision: 0,
          rules: [{ required: true, message: '线路时间不能为空', trigger: 'blur' }]
        },
        {
          type: 'INPUT_NUMBER',
          label: '线路里程(KM)',
          fieldName: 'wayMileage',
          placeholder: '请输入',
          min: 1,
          max: 999,
          precision: 2,
          rules: [{ required: true, message: '线路里程不能为空', trigger: 'change' }]
        },
        {
          type: 'INPUT',
          label: '描述',
          fieldName: 'wayDescribe',
          selfCols: 2,
          maxlength: 100,
          placeholder: '请输入',
          rules: [{ required: true, message: '描述不能为空', trigger: 'change' }]
        },
        {
          type: 'INPUT',
          label: '备注',
          fieldName: 'remark',
          selfCols: 2,
          maxlength: 100,
          placeholder: '请输入',
          rules: [{ required: true, message: '备注不能为空', trigger: 'change' }]
        },
        {
          type: 'INPUT_NUMBER',
          label: '排序',
          fieldName: 'rank',
          placeholder: '请输入',
          editType: 'number',
          precision: 0,
          rules: [{ required: true, message: '排序不能为空', trigger: 'blur' }]
        },
        {
          type: 'UPLOAD_IMG',
          label: '上传路线图',
          fieldName: 'wayPicture',
          placeholder: '请上传路线图',
          // style: { width: '250px' },
          rules: [{ required: true, message: '请上传路线图', trigger: 'change' }],
          upload: {
            actionUrl: '/api/file/oss/upload',
            fixedSize: [5, 3],
            isCalcHeight: true,
          }
        }
      ];
    },
    formChangeHandle(val) {
      this.saveData = val;
    },
    async saveDialog() {
      this.$refs.formPanel.SUBMIT_FORM();
      if (this.saveData !== null) {
        let res = {};
        // let data = {
        //   wayName: this.saveData.wayName,
        //   wayPicture: this.saveData.wayPicture[0],
        //   rank: this.saveData.rank,
        //   isValid: this.saveData.isValid,
        // }
        this.saveData.wayPicture = this.saveData.wayPicture[0]
        if (this.type === 'add') {
          res = await addTestDriveWay(this.saveData);
        }
        if (this.type === 'edit') {
          res = await editTestDriveWay(this.saveData, this.recordId);
        }
        if (res.resultCode === 200) {
          this.closeDialog('refresh');
          this.$message.success("保存成功!");
        }
      }
    },
    closeDialog(param) {
      this.$emit('close', false, param);
    }
  }
};
</script>

<style lang="less" scoped>
</style>
